using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

public class Player : Entity
{
    [Header("攻击详情 Attack details")]
    [Tooltip("攻击时位移")]
    public Vector2[] attackMovement;
    public float counterAttackDuration = .2f;

    [Header("移动信息 Move info")]
    [Tooltip("移动速度")]
    public float moveSpeed = 5f;
    [Tooltip("跳跃高度")]
    public float JumpForce = 15f;
    [Tooltip("剑返回力度")]
    public float swordReturnImpact = 5f;
    private float defaultMoveSpeed = 5f;//默认移动速度
    private float defaultJumpForce = 15f;//默认跳跃高度


    [Header("冲刺信息 Dash info")]//冲刺信息
    [Tooltip("冲刺速度")]
    public float dashSpeed = 25f;
    [Tooltip("冲刺时间")]
    public float dashDuration = 0.25f;
    [Tooltip("冲刺方向")]
    public float dashDir { get; private set; }
    private float defaultDashSpeed = 25f;//默认冲刺速度


    public bool isBusy { get; private set; }   //角色是否正忙
    public SkillManager skill { get; private set; } //技能管理
    public GameObject sword { get; private set; } //剑的管理

    #region States 状态

    public PlayerStateMachine stateMachine { get; private set; }//状态机
    public PlayerIdleState idleState { get; private set; }//待机状态
    public PlayerMoveState moveState { get; private set; }//移动状态
    public PlayerAirState airState { get; private set; }//空中状态
    public PlayerJumpState jumpState { get; private set; }//跳跃状态
    public PlayerDashState dashState { get; private set; }//冲刺状态
    public PlayerWallSlideState wallSlide { get; private set; }//攀爬状态
    public PlayerWallJumpState wallJump { get; private set; }//墙跳状态
    public PlayerPrimaryAttackState primaryAttack { get; private set; }//攻击状态
    public PlayerCounterAttackState counterAttack { get; private set; }//格挡状态
    public PlayerAimSwordState aimSword { get; private set; }//瞄准剑状态
    public PlayerCatchSwordState catchSword { get; private set; }//捕获剑状态
    public PlayerBlackholeState blackHole { get; private set; }//黑洞状态
    public PlayerDeadState deadState { get; private set; }//死亡状态


    #endregion

    protected override void Awake()
    {
        base.Awake();
        stateMachine = new PlayerStateMachine();//初始化状态机
        #region 状态初始化

        idleState = new PlayerIdleState(this, stateMachine, "Idle");//初始化待机状态
        moveState = new PlayerMoveState(this, stateMachine, "Move");//初始化移动状态
        airState = new PlayerAirState(this, stateMachine, "Jump");//初始化空中状态
        jumpState = new PlayerJumpState(this, stateMachine, "Jump");//初始化跳跃状态
        dashState = new PlayerDashState(this, stateMachine, "Dash");//初始化冲刺状态
        wallSlide = new PlayerWallSlideState(this, stateMachine, "WallSlide");//初始化攀爬状态
        wallJump = new PlayerWallJumpState(this, stateMachine, "Jump");//初始化墙跳状态
        primaryAttack = new PlayerPrimaryAttackState(this, stateMachine, "Attack");//初始化攻击状态
        counterAttack = new PlayerCounterAttackState(this, stateMachine, "CounterAttack");//初始化格挡状态
        aimSword = new PlayerAimSwordState(this, stateMachine, "AimSword");//初始化瞄准剑状态
        catchSword = new PlayerCatchSwordState(this, stateMachine, "CatchSword");//初始化捕获剑状态
        blackHole = new PlayerBlackholeState(this, stateMachine, "Jump");//初始化黑洞状态
        deadState = new PlayerDeadState(this, stateMachine, "Die");//初始化死亡状态

        #endregion


    }

    protected override void Start()
    {
        base.Start();
        skill = SkillManager.instance;// 技能管理器
        stateMachine.Initialize(idleState);// 状态机初始化状态

        // 设置默认值
        defaultMoveSpeed = moveSpeed;
        defaultJumpForce = JumpForce;
        defaultDashSpeed = dashSpeed;
    }

    protected override void Update()
    {
        if (Time.timeScale == 0) return;
        base.Update();
        stateMachine.currentState.Update();// 状态机执行状态
        CheckForDashInput();// 随时进入冲刺状态

        if (Input.GetKeyDown(KeyCode.F) && skill.crystal.crystalUnlocked)
        {
            skill.crystal.CanUseSkill();
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Inventory.instance.UseFlask();
        }
    }

    // 寒冷减速
    public override void SlowEntityBy(float _slowPercentage, float _slowDuration)
    {
        moveSpeed = moveSpeed * (1 - _slowPercentage);
        JumpForce = JumpForce * (1 - _slowPercentage);
        dashSpeed = dashSpeed * (1 - _slowPercentage);
        anim.speed = anim.speed * (1 - _slowPercentage);

        Invoke("ReturnDefaultSpeed", _slowDuration);
    }
    // 变为正常状态
    protected override void ReturnDefaultSpeed()
    {
        base.ReturnDefaultSpeed();
        moveSpeed = defaultMoveSpeed;
        JumpForce = defaultJumpForce;
        dashSpeed = defaultDashSpeed;
    }

    //重写死亡，进入死亡状态
    public override void Die()
    {
        base.Die();
        stateMachine.ChangeState(deadState);
    }

    //创造剑时让玩家获取剑的实例
    public void AssignNewSword(GameObject _newSword)
    {
        sword = _newSword;
    }
    //收剑碰到角色时进入动画并摧毁剑
    public void CatchTheSword()
    {
        stateMachine.ChangeState(catchSword);
        Destroy(sword);
    }

    // 协程方法，delay，但不影响主进程，让角色进入忙状态一定时间
    public IEnumerator BusyFor(float _second)
    {
        isBusy = true;
        yield return new WaitForSeconds(_second);
        isBusy = false;
    }

    // 动画结束事件/标志
    public void AnimationFinishTrigger() => stateMachine.currentState.AnimationFinishTrigger();

    // 冲刺输入检测
    private void CheckForDashInput()
    {
        if (IsWallDetected()) return;// 碰墙禁止冲刺-不是很必要的玩意

        if (!skill.dash.dashUnlocked) return;

        if (Input.GetKeyDown(KeyCode.LeftShift) && SkillManager.instance.dash.CanUseSkill())// 冷却完毕按下shift
        {

            dashDir = Input.GetAxisRaw("Horizontal"); // 冲刺时按下的方向
            if (dashDir == 0)
            {
                dashDir = facingDir;// 不按方向就按脸的方向冲刺
            }
            stateMachine.ChangeState(dashState);// 转为冲刺状态
        }
    }

    protected override void SetupZeroKnockbackPower()
    {
        knockbackDirection = new Vector2(0, 0);
    }

}
